class ServerInfo {
    constructor({
                    id = '',
                    position = '',
                    serverNumber = '',
                    serverName = '',
                    ip = '',
                    useUser = '',
                    standard = '',
                    model = '',
                    os = '',
                    cpu = '',
                    memoryDesc = '',
                    diskDesc = '',
                    ncNumber = '',
                    useDate = '',
                    terminateDate = '',
                    comments = '',
                    area = '',
                    frame = '',
                    house = '',
                } = {}) {
        this.id = id;
        this.serverNumber = serverNumber;
        this.serverName = serverName;
        this.ip = ip;
        this.useUser = useUser;
        this.standard = standard;
        this.model = model;
        this.os = os;
        this.cpu = cpu;
        this.memoryDesc = memoryDesc;
        this.diskDesc = diskDesc;
        this.ncNumber = ncNumber;
        this.useDate = useDate;
        this.terminateDate = terminateDate;
        this.comments = comments;
        this.position = position;
        this.area = area;
        this.frame = frame;
        this.house = house;
    };
};


new BaseValidateVue({
    el: '#app',
    data: function () {
        return {
            formData: new ServerInfo(),
            positions: [],
            customers: [],
            houses: [],
            areas: [],
            frames: [],
            ruleList: [
                new ValidateObject({
                    ruleName: 'server_number_validate',
                    validate: this.serverNumberValidate,
                }),
            ],
        }
    },
    methods: {
        createValidateList() {
            return this.ruleList;
        },
        async submitForm() {
            if (await this.$validator.validateAll() && await ajaxOption({
                res: baseServerInfoApi.add(this.formData),
                danger: '新增失败',
                success: '新增成功',
            })) {
                forward('/basic-data/admin_servers')
            } else {
                rxPrompt.dangerMsg("输入存在错误！");
            }
        },
        async initCustomers() {
            this.customers = await baseCustomerApi.selectList()
        },
        async initHouses() {
            this.houses = await baseHouseInfoApi.selectList()
        },
        async serverNumberValidate(val, args) {
            const {position = ''} = this.formData;
            if (position === '' || val === '') {
                return true;
            }
            return await baseServerInfoApi.selectServiceNumByPositionId(position, val);
        },
    },
    watch: {
        'formData.house': async function (newVal, oldVal) {
            this.areas = await baseIdcHouseAreaApi.selectListByParam({houseId: newVal});
            this.frames = '';
            this.positions = '';
        },
        'formData.area': async function (newVal, oldVal) {
            this.frames = await baseFrameInfoApi.selectListByParam({houseArea: newVal});
            this.positions = '';
        },
        'formData.frame': async function (newVal, oldVal) {
            this.positions = await basePositionApi.selectListByParam({frameId: newVal})
        },
    },
    async mounted() {
        this.initCustomers()
        this.initHouses()
    },
})

